
from dao import Dao

from model.item import Item
from dao.house import HouseDao

class ItemDao(Dao):
    """This the base class for AdvertisementDao, PictureDao
    """
    def __init__(self, ModelClass=Item):
        Dao.__init__(self, ModelClass)
        self.collection = 'items'
        
        self.items = {'house':['houses', HouseDao]}
    """
    def __init__(self, ModelClass=Item):
        Dao.__init__(self, ModelClass)
    
    def get_items(self):
        pass
    
    def add_to_db(self, item, user, category):
        user.items.append(item)
        category.items.append(item)
        self.session.add(item)
        self.session.commit()

    def remove_item(self, item):
        self.session.delete(item)
        self.session.commit()
    
    def update_item(self, _id, name, _type, description, price, quantity, status):
        _item = self.session.query(Item).get(_id)
        _item.name = name
        _item.type = _type
        _item.description = description
        _item.price = price
        _item.quantity = quantity
        _item.status = status
        
        self.session.commit()
        return _item
        
    def all(self, order_by):
        '''Get all rows
        '''
        try:
            return self.session.query(Item).order_by(order_by)
        except NoResultFound:
            return None
    
    def filter(self, *criterion):
        '''
        Arguments:
            criterion -- list
        '''
        return self.session.query(Item).filter(criterion).first()

    
    def filter_by(self, *args, **kwargs):
        '''
        Arguments:
            kwargs -- dict type
        '''    
        return self.session.query(Item).filter_by(**kwargs)
    
    def filter_first(self, *args, **kwargs):
        ''' filter and get the first
        Arguments:
            kwargs -- dict type
        '''    
        return self.session.query(Item).filter_by(**kwargs).first()

    """